package com.bjsxt.controller;


import com.bjsxt.pojo.Company;
import com.bjsxt.pojo.HostAddResult;
import com.bjsxt.pojo.PageResult;
import com.bjsxt.service.ICompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.LocalDateTime;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2021-10-14
 */
@Controller
@RequestMapping("/company")
public class CompanyController {
    @Autowired
    private ICompanyService companyService;
    //分页加载公司信息
    @ResponseBody
    @RequestMapping("companyInfo")
    public PageResult<Company> companyInfo(Integer page,Integer rows,String cname,String status,String ordernumber){
        //处理请求
        PageResult<Company> pageResult= companyService.selCompanyInfoService(page,rows,cname,status,ordernumber);
        //响应结果
        return pageResult;
    }

    //增加公司信息
    @ResponseBody
    @RequestMapping("companyAdd")
    public HostAddResult companyAdd(Company company){
        company.setStarttime(LocalDateTime.now());
        company.setStatus("1");
        company.setOrdernumber(0);
        boolean insert = company.insert();
        return new HostAddResult(insert,insert?"添加成功":"添加失败");
    }
    //更新数据
    @ResponseBody
    @RequestMapping("companyEdit")
    public HostAddResult companyEdit(Company company){
        boolean insert = company.updateById();
        return new HostAddResult(insert,insert?"更新成功":"更新失败");
    }

    //更新状态
    @ResponseBody
    @RequestMapping("companyAccountUp")
    public String companyAccountUp(String cids,String statuss){
        //处理请求
            //获取要修改的公司ID数组
        String[] cidstr = cids.split(",");
        String[] statusstr = statuss.split(",");
        Company company = new Company();
        boolean b = true;
        //修改状态
        for(int i = 0 ;i < cidstr.length;i++){
            company.setCid(Integer.parseInt(cidstr[i]));
            company.setStatus("1".equals(statusstr[i])?"0":"1");
            b = company.updateById();
        }

        return  b?"1":"0";
    }
}

